package cn.quan.dlairport;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ListView;

/**
 * 
 * @author quanyf@gmail.com
 * 
 */
public class SettingActivity extends Activity {

	//view
	private ListView mListLocal=null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG,"SettingActivity onCreate");
        setContentView(R.layout.setting);

        List<String> locale = new ArrayList<String>();
        makeLocalList(locale);
        if(locale.size()>0){
	        mListLocal = (ListView)findViewById(R.id.listView1);
	
	        SettingAdapter adapter = new SettingAdapter(
	        		this,
	        		R.layout.setting_list_item_1,
	        		locale
	        		);
	
	        mListLocal.setAdapter(adapter);
        }
        Log.v(TAG,"onCreate end");

    }
    private void makeLocalList(List<String> locale){
		//file check
		if(!Environment.getExternalStorageState().contains("mounted")){
	        Log.v(TAG,"makeLocalList no mounted");
			return ;
		}
		String flightDir = Environment.getExternalStorageDirectory().getPath()+java.io.File.separator+DlairportActivity.FLIGHT_DIR;
		String flightFile = flightDir+java.io.File.separator+DlairportActivity.FLIGHT_FILE;
		File file = new File(flightFile);
		if(!file.exists()){
	        Log.v(TAG,"makeLocleList file not exists");
			return;
		}
		//load data
		String line=null;
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			String[] data=null;
            while((line = br.readLine()) != null){ 
            	data = line.split(",");
            	//set data
            	if(data.length>8){
                	String[] loc = new String[2];
            		loc[0]=data[1].trim();//from
            		loc[1]=data[2].trim();//to
            		for(int j=0;j<loc.length;j++){
                		if(loc[j].length()>0){
                			if(locale.indexOf(loc[j])>=0){
                			}else{
                				if(loc[j].indexOf(LOCAL_DALIAN)<0){
	                				locale.add(loc[j]);
	                				Log.v(TAG,"*Locale:"+loc[j]);
                				}
                			}
                		}
            		}
            		
            	}else{
            		//skip
            	}
            }

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	private final static String LOCAL_DALIAN = "大连";
    private final String TAG = "**FLIGHT";

}